package com.sxd.bill.rela.item;

import lombok.Data;
import java.util.Map;

import com.sxd.bill.common.enums.LabelNameEnum;

import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.lang.reflect.Field;

@Data
public class CoreRelationItem {
    protected List<String> discounter; // 贴现人
    protected List<String> acceptor; // 承兑人
    protected List<String> dueTime; // 票据到期时间
    protected String tradingDirection; // 交易方向
    protected String rate; // 利率
    protected String amt; // 金额
    protected String extractingText; // 抽取的文本
    protected String extractMethod; // 抽取方法
    protected List<String> billType; // 票据类型

    public CoreRelationItem() {
        this.discounter = new ArrayList<String>();
        this.acceptor = new ArrayList<String>();
        this.dueTime = new ArrayList<String>();
    }

    private static final Map<LabelNameEnum, String> fieldMap = new HashMap<>();
    static {
        fieldMap.put(LabelNameEnum.ACCEPTOR, "acceptor");
        fieldMap.put(LabelNameEnum.DISCOUNTER, "discounter");
        fieldMap.put(LabelNameEnum.AMOUNT, "amt");
        fieldMap.put(LabelNameEnum.DUE_TIME, "dueTime");
        fieldMap.put(LabelNameEnum.RATE, "rate");
        fieldMap.put(LabelNameEnum.TRD_DIR, "tradingDirection");
    }

    public void addDueTime(String dueTime) {
        this.dueTime.add(dueTime);
    }

    public void addAcceptor(String acceptor) {
        this.acceptor.add(acceptor);
    }

    public void addDiscounter(String discounter) {
        this.discounter.add(discounter);
    }

    public void update(LabelNameEnum labelName, Object entity) {
        String field = fieldMap.get(labelName);
        if (field != null) {
            try {
                Field declaredField = getClass().getDeclaredField(field);
                declaredField.setAccessible(true);
                Object fieldValue = declaredField.get(this);
                if (fieldValue instanceof List) {
                    List<String> list = (List<String>) fieldValue;
                    if (entity instanceof List) {
                        list.addAll((List<String>) entity);
                    } else if (entity instanceof String) {
                        list.add((String) entity);
                    }
                } else if (entity instanceof String) {
                    declaredField.set(this, entity);
                } else {
                    System.out.println("update failed -> fieldName:" + field + " fieldValue: " + entity
                            + " fieldValue Type is String:" + (fieldValue instanceof String)
                            + " entity type is String: "
                            + (entity instanceof String));
                }
            } catch (NoSuchFieldException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    public static CoreRelationItem fromMap(Map<String, Object> map) {
        CoreRelationItem item = new CoreRelationItem();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            try {
                LabelNameEnum newKey = LabelNameEnum.fromEnName(key);
                item.update(newKey, entry.getValue());
            } catch (Exception e) {
                e.printStackTrace();
                continue;
            }

        }
        return item;
    }
}
